;define(function (require) {
	'use strict';
	var $=require('jquery');
	
	require('compare/superslide');
	require('compare/contextmenu');
	require('layer');
	require('compare/plugin-mapNt-extend');
	
	var echart3=require('echarts');
	var config = require('config');
	var $Fun = require('functions');
	
//	var reqConfig = {};
//	if(config.debugModel){
//		reqConfig = config.debugAjaxRequestUrls;//测试数据
//	}else{
//		reqConfig = config.urlConfig;//接口配置
//	}
	
	//地图工具
	function mapMaker(){};
	mapMaker.prototype = {
		init:function(){
			//TODO:从接口获取数据
			//name指标名称,zbKey指标对应唯一标识、用来获取具体数据的参数
			this.zbList = [{"name":"项目数","zbKey":"zb01"}
			,{"name":"项目状态投资情况","zbKey":"zb02"}
			,{"name":"项目类型投资情况","zbKey":"zb03"}

//			,{"name":"工业用电量","zbKey":"zb06"}
//			,{"name":"进出口总值","zbKey":"zb07"}
//			,{"name":"出口总值","zbKey":"zb08"}
//			,{"name":"固定资产投资","zbKey":"zb09"}
			];
			
			//value增量,valueDay日增量,per增幅,perDay日增幅
			//江苏省各市数据，这里的数据需要根据上面数组中的参数获取的，是指定某个指标的数据
			this.data = [{"name":"苏州市","value":545,"valueDay":54,"per":80,"perDay":8},
						{"name":"南京市","value":337,"valueDay":33,"per":70,"perDay":7},
						{"name":"无锡市","value":274,"valueDay":27,"per":68,"perDay":7},
						{"name":"南通市","value":189,"valueDay":18,"per":65,"perDay":7},
						{"name":"徐州市","value":168,"valueDay":16,"per":60,"perDay":6},
						{"name":"盐城市","value":161,"valueDay":16,"per":60,"perDay":6},
						{"name":"常州市","value":145,"valueDay":14,"per":50,"perDay":5},
						{"name":"淮安市","value":120,"valueDay":12,"per":48,"perDay":4},
						{"name":"宿迁市","value":107,"valueDay":10,"per":40,"perDay":4},
						{"name":"泰州市","value":104,"valueDay":10,"per":40,"perDay":4},
						{"name":"连云港市","value":100,"valueDay":10,"per":40,"perDay":4},
						{"name":"镇江市","value":94,"valueDay":9,"per":35,"perDay":3},
						{"name":"扬州市","value":83,"valueDay":8,"per":30,"perDay":3}];
			return this;
		}
	}
	var mapMakerObj = new mapMaker().init();
	//项目初始化
	var app = {};
	//初始化
	app.init = function(){
		//十六进制转rgba
		String.prototype.color2Rgba=function(){var sColor=this.toLowerCase(),reg=/^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;if(sColor&&reg.test(sColor)){if(sColor.length===4){var sColorNew="#";for(var i=1;i<4;i+=1){sColorNew+=sColor.slice(i,i+1).concat(sColor.slice(i,i+1))}sColor=sColorNew}var sColorChange=[];for(var i=1;i<7;i+=2){sColorChange.push(parseInt("0x"+sColor.slice(i,i+2)))}return"RGBA("+sColorChange.join(",")+","}else{return sColor}};
		
		//更新最大最小值
		updateMapDataMaxMin();
		
		//初始化指标待选项,从mapMakerObj.zbList获取
		create_zbHtml();
		
		//初始化手风琴
		initShouFengQin(1);
		
		//layer
		$('.tw-right-mark').off('click').on('click',function(){
			var $this = $(this);
			if($this.hasClass('open')){
				closeTogglePage();
			}else{
				openTogglePage();
			}
			
			//事件
			toggleOutPage();
		})
		
		//首次运行初始化注册地图组件
		$Fun.getJsonData(config.mapConfig.urlJsMap, function(returnJson){
			echart3.registerMap('jiangsu', returnJson);
			//标记下当前等级
			mapMakerObj.level = 1;
			var mapChart = echart3.init($('#mapJs')[0]);
			
			bindMapEvents({
				mapChart:mapChart
			});
			
			//右键菜单事件
			$('#mapJs').contextMenu('myMenu1', {
				//part-new-select
				onContextMenu: function(e) {
					if(mapMakerObj.level==99){
						return false;
					}else{
						return true;
					}
			    },
			    //end
			    
		        bindings: {
		        	//加入对比
			        'joinDB': function(t) {
			        	//取出临时列表保存
						//mapMakerObj.dbList = mapMakerObj.tmpList;
						//mapMakerObj.tmpList = {};
			        	if(!mapMakerObj || !mapMakerObj.tmpSelected){
			        		layer.msg('请至少选中一个地区',{icon:5});
			        		return false;
			        	}
			        	
			        	if(!mapMakerObj.dbList){
			        		mapMakerObj.dbList = {};
			        	}
			        	
			        	var params = mapMakerObj.tmpSelected;
			        	
			        	if(mapMakerObj.dbList[params.name]){
			        		layer.msg('已存在列表清单中',{icon:5});
			        		return false;
			        	}
			        	
			        	mapMakerObj.dbList[params.name] = params;
						
						var data = [];
						$.each(mapMakerObj.dbList, function(i,e) {
							data.push(e.data);
//							data.push({"name":i,"value":e.value});
						});
						
						//地区添加至弹出窗
						addAreas(data);
						
						//弹窗
						openTogglePage();

						//事件
						toggleOutPage();
						
						
						//绑定删除事件
						bindAreaDel();
						
						//hide now
						//createMapBar(jsons.Data);
							
						mapMakerObj.nowClick = false;
			        }
			        //地图下钻
			        ,'enterDB': function(t) {
			        	//part-new-select
			        	$.fn.clearMapNtChildTabs();//end
			          	openMap(checkedMapAll);
			          	mapMakerObj.nowClick = false;
			        }
			        //地图上钻
			        ,'returnDB': function(t) {
			        	//part-new-select
			        	$.fn.clearMapNtChildTabs();//end
			          	backMap(checkedMapAll);
			          	mapMakerObj.nowClick = false;
			        }
		        }
		    });
		        
			//首次运行触发更新地图
			firstRun();
			
			openMapToNanTong(checkedMapAll);
		})
		
				
		//TODO:获取接口数据
		//ajax获取指标对应数据的函数，所有更新数据都写在这
		function updateJsonData(){
			var $titon = $('.JQ_shoufengqin .tw-htitle.on');
			if($titon && $titon.length>0){
				var zbKey = $titon.attr('zbKey');//zb01
				//这里做ajax操作，获取默认指标的数据，更新mapMakerObj对象
				//return回来rtnData
				
//				mapMakerObj.data = rtnData;

//				需要回调中更新最大最小值
				updateMapDataMaxMin();
			}
		}
		//tab切换
		function updateJsonDataTab(){
			//选中的指标
			var $lion = $('.tw-tabMap ul li.on');
			if($lion && $lion.length>0){
				var zbKey = $lion.attr('zbKey');//zb01
				//选中的地区
				var dbList = mapMakerObj.dbList;
				var arr = [];
				$.each(dbList, function(i,e) {
					var tname = i;
					//?zbKey=zbKey&tname=tname
					//TODO:ajax获取数据
					//e = return
					arr.push(e);
				});
				mapMakerObj.zbType.arearData = arr;
			}
		}
		
		//更新数据的最大最小值
		function updateMapDataMaxMin(){
			mapMakerObj.max = 82;
			mapMakerObj.min = 38;
		}
		
		//渲染可选指标项
		function create_zbHtml(){
			var zbList = mapMakerObj.zbList;
			var zbHtml = '';
			$.each(zbList, function(i,e) {
				//默认勾选前4个指标
				if(i<4){
					var ckd = 'checked="checked"';
				}else{
					var ckd = '';
				}
				zbHtml += '<label for="">\
						<input type="checkbox" name="'+e.zbKey+'" '+ckd+' />\
						<span>'+e.name+'</span>\
					</label>';
			});
			$('.tw-alertPage .bodyer').empty().append(zbHtml);
		}
		
		function toggleOutPage(){
			//重新绑定删除事件
			bindAreaDel();
			
			var newList = [];
			//同步input
			$('.tw-alertPage').find('input[type=checkbox]').off('change').on('change',function(){
				var $this = $(this);
				var checked = $this.is(':checked');
				if(checked){
					$('input[name='+$this.attr('name')+']').attr('checked','checked');
				}else{
					$('input[name='+$this.attr('name')+']').removeAttr('checked');
				}
			})
			
			//点击对比按钮
			$('.tw-alertPage .header .btn').off('click').on('click',function(){
				//mapMakerObj.zbType.areaData = reset
				if(mapMakerObj && mapMakerObj.dbList){
					
				}else{
					layer.msg('请先选择地区参数',{icon:5});
					return false;
				}
				
				var dblist = mapMakerObj.dbList;
				var redata = [];
				$.each(dblist, function(i,e) {
//					var tmp = mapMakerObj.list[i];
//					redata.push({
//						name:i,
//						value:tmp.value,
//						valueDay:tmp.valueDay,
//						per:tmp.per,
//						perDay:tmp.perDay
//					});
					redata.push(e.data);
				});
				
				if(redata && redata.length>0){
					
				}else{
					layer.msg('请至少选择一个地区参数',{icon:5});
					return false;
				}
				mapMakerObj.zbType.areaData = redata;
				mapMakerObj.nowData = mapMakerObj.zbType;
				
				//again 绑定手风琴事件
				initShouFengQin(2);
				//end
				
				closeTogglePage();
				
				//装载新的tab弹窗
				layer.open({
				    title: '指标对比',
				    btn:[],
				    shade:0.1,
				    area:['960px','635px'],
				    scrollbar: false,
				    content: '<div class="JQ_outLayer"></div>',
				    success:function(){
				    	//加载tab标签页内容并绑定事件
				    	var obj = $('.tw-alertPage').find('input[type=checkbox]:checked');
				    	loadNewLayer(obj);
				    }
				});
			})
		}
		
		function openTogglePage(){
			$('.tw-right-mark').addClass('open');
			$('.tw-contrainWrap').show();
			$('.tw-alertPage').show();
//			console.log($('.tw-right-mark').offset());
			var mTop = $('.tw-right-mark')[0].offsetTop;
			$('.tw-alertPage').css({top:mTop+3});
		}
		
		function closeTogglePage(){
			$('.tw-right-mark').removeClass('open');
			$('.tw-alertPage').hide();
		}
		
		
		function createShouFengQinHtml(){
			//生成手风琴start
			var obj = $('.tw-alertPage').find('input[type=checkbox]:checked');
			var html = '';
			$.each(obj, function(i,e) {
				var $span = $(e).next('span');
				var txt = $span.text();
				html += '<div class="tw-htitle" zbKey="'+e.name+'">\
					<span class="tit">'+txt+'</span>\
					<span class="icon"></span>\
				</div>';
				html += '<div class="tw-chart-area" style="display: none;">\
					<div class="tw-chart-div"></div>\
					<div class="tw-chart-span"></div>\
				</div>';
			});
			$('.JQ_shoufengqin').empty().append(html);
		}
		
		function initShouFengQin(num){
			createShouFengQinHtml();
			//手风琴指标
			jQuery(".JQ_shoufengqin").slide({
				titCell:".tw-htitle",
				targetCell:".tw-chart-area",
				defaultIndex:0,
				effect:"fade",
				delayTime:300,
				returnDefault:false,
				trigger:"click",
				defaultPlay:true,
				startFun:function(a,b,c,d,e,f){
					$($(f)[a]).addClass('active').siblings('.tw-chart-area').removeClass('active');
				},
				endFun:function(a,b,c,d,e,f){
					if(num>1){
						var data = mapMakerObj.lastClickData;
						createMapBar(data);
					}else{
						updateJsonData();
						var data = {
							"areaData":mapMakerObj.data,
							"colors":["#9bd4f1"],
							"map":"jiangsu",
							"mapName":"江苏",
							"max":mapMakerObj.max,
							"min":mapMakerObj.min};
						if(data){
							createMixedChart(data, '.tw-chart-area.active .tw-chart-div');
						}
						//createMapBarLayer(data);
					}
				}
			});
		}
		
		function loadNewLayer(obj){
			var $layer = $('.JQ_outLayer');
			var zbs = '';
			var zbodys = '';
			$.each(obj, function(i,e) {
				var $span = $(e).next('span');
				var txt = $span.text();
				zbs += '<li zbKey='+e.name+'><span class="topline"></span><a href="javascript:;">'+txt+'</a></li>';
				zbodys += '<div class="botBody"></div>';
			});
			var html = '<div class="tw-tabMap">\
							<ul class="clearfix">'+zbs+'</ul>\
						</div>\
						<div class="tw-tabBody">'+zbodys+'</div>';
			$layer.empty().append(html+'<div class="left"></div><div class="right"></div>');
			//绑定tab切换事件
			bindLayerTabs();
		}
		
		function bindLayerTabs(){
			$('.JQ_outLayer').slide({
				titCell:'.tw-tabMap ul li',
				trigger:'click',
				mainCell:'.tw-tabBody',
				startFun:function(a,b,c,d,e){
					$(e).find('.botBody').eq(a).addClass('active').siblings('.botBody').removeClass('active');
				},
				endFun:function(a,b,c,d,e){
					if(mapMakerObj && mapMakerObj.dbList){
						//againRun();
						//var data = mapMakerObj.nowData;
						//createMapBarLayer(data);
						updateJsonDataTab();
						var data = mapMakerObj.zbType;
						createMixedChart(data, '.botBody.active');
					}else{
						//firstRun();
					}
				}
			})
			//left
			$('.JQ_outLayer .right').off('mousedown').on('mousedown',function(){
				var $ul = $('.JQ_outLayer .tw-tabMap >ul');
				var leftNum = parseFloat($ul.css('left'));
				if($ul.width()>560){
					var min = -($ul.width() - 560);
					if(leftNum<min){
						$ul.css('left',min+'px');
						clearInterval(mapMakerObj.timerL);
					}else{
						mapMakerObj.timerL = setInterval(function(){
							leftNum = leftNum - 20;
							if(leftNum<min){
								$ul.css('left',min+'px');
								clearInterval(mapMakerObj.timerL);
							}else{
								$ul.css('left',leftNum+'px');
							}
						},50);
					}
				}
			})
			$('.JQ_outLayer .right').off('mouseup').on('mouseup',function(){
				clearInterval(mapMakerObj.timerL);
			})
			
			//right
			$('.JQ_outLayer .left').off('mousedown').on('mousedown',function(){
				var $ul = $('.JQ_outLayer .tw-tabMap >ul');
				var leftNum = parseFloat($ul.css('left'));
				if($ul.width()>560){
					if(leftNum<=0){
						mapMakerObj.timerR = setInterval(function(){
							leftNum = leftNum + 20;
							if(leftNum>=0){
								clearInterval(mapMakerObj.timerR);
								$ul.css('left','0px');
							}else{
								$ul.css('left',(leftNum)+'px');
							}
						},50);
					}else{
						$ul.css('left','0px');
						clearInterval(mapMakerObj.timerR);
					}
				}
			})
			$('.JQ_outLayer .left').off('mouseup').on('mouseup',function(){
				clearInterval(mapMakerObj.timerR);
			})
		}
		
		//地图根据指标初始化
		function mapInit(params){
			var data = params.data;
			
			//缓存起来
			mapMakerObj.zbType = data;//指标类型
			//画地图
			$Fun.drawChartMap( '#mapJs', data, function(rtn){
				//获取地图实例
				var echartIsc = echart3.getInstanceByDom($('#mapJs')[0]);
				//更新数据
				var option = rtn;
				//fixed colors
				//option.visualMap.inRange.color = $Fun.getOpacityColors(('#c65152').color2Rgba()).reverse();
				option.visualMap.inRange.color = ['#c65152','#d98172','#df9d81','#e3ab87','#e7b98d','#edcd95','#f3e5a1','#fff8d7'].reverse();
				option.series[0].zoom = 1.2;
				echartIsc.setOption(option, true);
				
				//初次绘图，江苏，混合图
				if(params.callback){
					params.callback();
				}
				
				//绑定区域tab切换事件
				$('.map-toolbar .tw-tabMap ul li').off('click').on('click',function(){
					var $this = $(this);
					var idx = $this.index();
					if(idx==0){//江苏
						$this.addClass('on').siblings('li').removeClass('on');
						backMapToJiangSu(checkedMapAll);
					}
					if(idx==1){//南通
						$this.addClass('on').siblings('li').removeClass('on');
						openMapToNanTong(checkedMapAll);
					}
				})
			})
		}
		
		function mapUpdate(params){
			var data = params.data;
			
			if(!mapMakerObj.list){
				mapMakerObj.list = {};
			}
			
			var newdata = [];
			$.each(data.areaData, function(i,e) {
//				console.log(i,e,mapMakerObj.list[e.name]);
				if(mapMakerObj.list[e.name]){
					newdata.push({name:e.name, value:e.value});
				}
			});
			data.areaData = newdata;
			
			//缓存起来
			mapMakerObj.zbType = data;//指标类型
			
			createMapBar(data);
		}
		
		function firstRun(){
			//指标名称
			var text = $('.tw-htitle.on .tit').text();
			//console.log(text);
			
			//缓存指标名称
			mapMakerObj.zbText = text;
			
			updateJsonData();
			
			var jsons = {
				"Data":{
					"areaData":mapMakerObj.data,//取的江苏省各市数据
					"colors":["#9bd4f1"],//006ecb
					"map":"jiangsu",
					"mapName":"江苏",
					"max":mapMakerObj.max,//550,
					"min":mapMakerObj.min},//80},
				"Success":true
			};
			
			if(!mapMakerObj.parDataList){
				mapMakerObj.parDataList = {};
			}
			mapMakerObj.parDataList['jiangsu'] = jsons.Data.areaData;
			
			//更新地图
			mapInit({
				data:jsons.Data,
				callback:function(){
					createMixedChart(jsons.Data, '.tw-chart-area.active .tw-chart-div');
				}
			});
		}
		
//		function againRun(){
//			//指标名称
//			var text = $('.tw-htitle.on .tit').text();
//			//console.log(text);
//			
//			//缓存指标名称
//			mapMakerObj.zbText = text;
//			
//			updateJsonData();
//			
//			var jsons = {
//				"Data":{
//					"areaData":mapMakerObj.parDataList['jiangsu'],
//					"colors":["#9bd4f1"],//006ecb
//					"map":"jiangsu",
//					"mapName":"江苏",
//					"max":mapMakerObj.max,//550,
//					"min":mapMakerObj.min},//80},
//				"Success":true
//			};
//			
//			//更新地图
//			mapUpdate({
//				data:jsons.Data
//			});
//		}
		
		
		function addAreas(data){
			var html = '';
			$.each(data,function(i,e){
				html += '<div class="item">\
					<span>'+e.name+'</span>\
					<span class="del"></span>\
				</div>';
			})
			$('.selectedAreas').empty().append(html);
		}
		
		function bindAreaDel(){
			$('.selectedAreas .del').off('click').on('click',function(){
				var $this = $(this),that=this;
				//1
				var name = $this.prev('span').text();
//				mapMakerObj.tmpList[name] = false;
//				mapMakerObj.dbList[name] = false;
				mapMakerObj.tmpSelected = false;
				//重组dblist
				var ndb = {};
				$.each(mapMakerObj.dbList, function(i,e) {
					if(name!==i){
						ndb[i] = e;
					}
				});
				mapMakerObj.dbList = ndb;
				
				//2
				var idx = $this.parent('.item').index();
				$('.selectedAreas').eq(0).find('.item').eq(idx).remove();
				$('.selectedAreas').eq(1).find('.item').eq(idx).remove();
				
				//3 change map option
				delMapChecked(name);
			})
		}
		
		function delMapChecked(name){
			var ecisc = echart3.getInstanceByDom($('#mapJs')[0]);
			var option = ecisc.getOption();
			var data = option.series[0].data;
			var newData = [];
			$.each(data, function(i,e) {
				if(e.name==name){
					var f = e;
					f.selected = false;
					newData[i] = f;
				}else{
					newData[i] = e;
				}
			});
			option.series[0].data = newData;	
			ecisc.setOption(option);
		}
		function addMapChecked(name){
			var ecisc = echart3.getInstanceByDom($('#mapJs')[0]);
			var option = ecisc.getOption();
			var data = option.series[0].data;
			var newData = [];
			$.each(data, function(i,e) {
				if(e.name==name){
					var f = e;
					f.selected = true;
					newData[i] = f;
				}else{
					newData[i] = e;
				}
			});
			option.series[0].data = newData;
			ecisc.setOption(option);
		}
		//上钻下钻回调事件
		function checkedMapAll(){
//			if(mapMakerObj && mapMakerObj.dbList){
//				var db = mapMakerObj.dbList
//				$.each(db, function(i,e) {
//					if(e){
//						addMapChecked(i);
//					}else{
//						delMapChecked(i);
//					}
//				});
//			}
//			//根据level切换tab
//			if(mapMakerObj.level==1){
//				$('.map-toolbar .tw-tabMap ul li').eq(0).addClass('on').siblings('li').removeClass('on');
//			}
//			if(mapMakerObj.level==2){
//				$('.map-toolbar .tw-tabMap ul li').eq(1).addClass('on').siblings('li').removeClass('on');
//			}
//			if(mapMakerObj.level>2){
//				$('.map-toolbar .tw-tabMap ul li').eq(2).addClass('on').siblings('li').removeClass('on');
//			}
		}
		
		function bindMapEvents(mapObject){
			var mapChart = mapObject.mapChart;
			//地图
			mapChart.on('click',function(params){
				
				//tmp select
				mapMakerObj.tmpSelected = params;
				
				//通过地区名称和指标名称获取数据并缓存
				var jsons = {
					"Data":{
						"areaData":[{"name":params.name,
							"value":params.value
//							,"valueDay":params.valueDay,
//							"per":params.per,
//							"perDay":params.perDay
						}],
						"colors":["#9bd4f1"],//006ecb
						"max":mapMakerObj.max,//550,
						"min":mapMakerObj.min},//80},
					"Success":true
				};
				
				if(!mapMakerObj.list){
					mapMakerObj.list = {};
				}
				mapMakerObj.list[params.name] = params.data;
				
				mapMakerObj.lastClickData = jsons.Data;
				
				createMapBar(jsons.Data);
			})
			
			//地区更改事件缓存地区
			mapChart.on('mapselectchanged',function(params){
//				if(!mapMakerObj.tmpList){
//					mapMakerObj.tmpList = {};
//				}
				//mapMakerObj.tmpList = $.extend(true, mapMakerObj.tmpList, params.selected);
				//mapMakerObj.dbList = $.extend(true, mapMakerObj.dbList, params.selected);
				//mapMakerObj.dbList = params.selected;
			})
			
			//记录鼠标当前在哪个区域，用于下钻
			mapChart.on('mouseup',function(params){
				mapMakerObj.nowClick = params;
			});
			mapChart.on('mouseover',function(params){
				mapMakerObj.tmpSelected = params;
				if(!mapMakerObj.list){
					mapMakerObj.list = {};
				}
				mapMakerObj.list[params.name] = params.data;
				mapMakerObj.nowClick = params;
			});
		}
		
		function openMap(callback){
			var paramObj = mapMakerObj.nowClick;
			if(!paramObj){
				layer.msg('请在地图内部区域适使用下钻功能',{icon:5});
				return false;
			}
			var name = paramObj.name;
			if( (mapMakerObj.level==1) && name!=='南通市' ){
				layer.msg('暂无该地区数据',{icon:5});
				return false;
			}else{
				//part-new-select
				if(name=='南通市'){
					showNanTongFourChild();
				}else{
					$.fn.clearMapNtChildTabs();
				}//end
				
				//记录父级
				if(!mapMakerObj.parentList){
					mapMakerObj.parentList = {};
				}
				mapMakerObj.parentList[name] = paramObj.seriesName;
				
				if(mapMakerObj.level==1){
					var url = config.mapConfig.urlCityPath + config.city2Code[name] + '.json';
				}
				if(mapMakerObj.level==2){
					//temp320602000000_mkt_ys.json
					var url = config.mapConfig.urlQxPath + 'temp' + config.mapName2Code[name] + '_mkt_ys.json';
				}
				if(mapMakerObj.level==3){
					//temp320602000000_mkt_ys.json
					var url = config.mapConfig.urlStreetPath + 'temp' + config.StreetName2Code[name] + '_mkt_ys.json';
				}
				if(mapMakerObj.level>3){
					layer.msg('暂无下级数据',{icon:5});
					return false;
				}
				var pinyin = $Fun.pinyin.getFullChars(name).toLowerCase();
				
				$Fun.getJsonData(url, function(returnJson){
					echart3.registerMap(pinyin, returnJson);
					
					mapMakerObj.level++;
					
					var ecisc = echart3.getInstanceByDom($('#mapJs')[0]);
					var option = ecisc.getOption();
					option.series[0].map = pinyin;
					option.series[0].name = name;
					
					//模拟数据
					var fs = returnJson.features;
					$.each(fs, function(i,e) {
						var pro = e.properties;
						var tname = pro.name;
//						option.series[0].data.push({
//							name:tname,
//							value:$Fun.getRandomNum(80,550),
//							valueDay:$Fun.getRandomNum(80,550),
//							per:$Fun.getRandomNum(80,550),
//							perDay:$Fun.getRandomNum(80,550)
//						});
						option.series[0].data.push(getAreaData(tname));
					});

					//记录
					mapMakerObj.parDataList[pinyin] = option.series[0].data;
					
					//街道名称隐藏
					if(mapMakerObj.level>=3){
						option.series[0].label.normal.show = false;
					}
					
					var mapChart = echart3.init($('#mapJs')[0]);
					mapChart.setOption(option);
					
					bindMapEvents({
						mapChart:mapChart
					});
					callback();
					
					if(mapMakerObj.level>=3){
						$('.map-toolbar .tw-tabMap ul li').eq(2).addClass('on').siblings('li').removeClass('on');
					}
					if(mapMakerObj.level==2){
						$('.map-toolbar .tw-tabMap ul li').eq(1).addClass('on').siblings('li').removeClass('on');
					}
				})
			}
		}
		
		function getAreaData(tname){
			//模拟的随机数据，需要替换成接口数据
			//TODO:ajax获取对应指标和对应地区的数据
			//console.log(tname);地区中文名称
			return {
					name:tname,
					value:$Fun.getRandomNum(38,82,0),
					valueDay:$Fun.getRandomNum(38,82,0),
					per:$Fun.getRandomNum(38,82,0),
					perDay:$Fun.getRandomNum(38,82,0)
				}
		}
		
		function openMapToNanTong(callback){
			var name = '南通市';
			//记录父级
			if(!mapMakerObj.parentList){
				mapMakerObj.parentList = {};
			}
			mapMakerObj.parentList[name] = '江苏';
			mapMakerObj.level = 2;
			var url = config.mapConfig.urlBasePath + config.map2code[name];
			var pinyin = $Fun.pinyin.getFullChars(name).toLowerCase();
			
			$Fun.getJsonData(url, function(returnJson){
				echart3.registerMap(pinyin, returnJson);
				
				var ecisc = echart3.getInstanceByDom($('#mapJs')[0]);
				var option = ecisc.getOption();
				option.series[0].map = pinyin;
				option.series[0].name = name;
				
				//模拟数据
				option.series[0].data = [];
				var fs = returnJson.features;
				$.each(fs, function(i,e) {
					var pro = e.properties;
					var tname = pro.name;
					option.series[0].data.push(getAreaData(tname));
				});

				//记录
				mapMakerObj.parDataList[pinyin] = option.series[0].data;
				
				option.series[0].label.normal.show = true;
				
				var mapChart = echart3.init($('#mapJs')[0]);
				mapChart.setOption(option);
				
				bindMapEvents({
					mapChart:mapChart
				});
				callback();
				//part-new-select
				showNanTongFourChild();//end
			})
		}
		
		//part-new-select
		function showNanTongFourChild(){
//			$.fn.createMapNtChildTabs({
//				whichObjToBeAftered:$('.map-toolbar'),
//				callback:function(chartMenuObj){
//					bindChartMenuObjEvent(chartMenuObj);
//				}
//			});
		}//end
		
		//part-new-select
		function bindChartMenuObjEvent(chartMenuObj){
			chartMenuObj.find('li').off('click').on('click',function(){
				var $this = $(this)
				,tid=$this.attr('data-tid')
				,name=$this.text();
				
				$this.addClass('on').siblings('li').removeClass('on');
					
				var url = config.mapConfig.urlCityPath +'/sea/' + config.zhen2Code[name];
				var pinyin = $Fun.pinyin.getFullChars(name).toLowerCase();
				
				$Fun.getJsonData(url, function(returnJson){
					echart3.registerMap(pinyin, returnJson);
					mapMakerObj.level = 99;	
					var ecisc = echart3.getInstanceByDom($('#mapJs')[0]);
					var option = ecisc.getOption();
					option.series[0].map = pinyin;
					option.series[0].name = name;
					
					//模拟数据
					var fs = returnJson.features;
					$.each(fs, function(i,e) {
						var pro = e.properties;
						var tname = pro.name;
						option.series[0].data.push(getAreaData(tname));
					});
	
					//记录
					mapMakerObj.parDataList[pinyin] = option.series[0].data;
					
					option.series[0].label.normal.show = false;
					
					var mapChart = echart3.init($('#mapJs')[0]);
					mapChart.setOption(option);
					
					bindMapEvents({
						mapChart:mapChart
					});
					
					//TODO:reset mapMakerObj.data
					initShouFengQin(1);
				})
			})
		}//end
		
		function backMap(callback){
			var paramObj = mapMakerObj.nowClick;
			if(!paramObj){
				layer.msg('请在地图内部区域适使用上钻功能',{icon:5});
				return false;
			}
			var name = paramObj.name;
			if(mapMakerObj.level==1){
				layer.msg('暂无上级数据',{icon:5});
				return false;
			}else{
				var targName = mapMakerObj.parentList[paramObj.seriesName];
				var targPinyin = $Fun.pinyin.getFullChars(targName).toLowerCase();
				
				mapMakerObj.level--;
				var ecisc = echart3.getInstanceByDom($('#mapJs')[0]);
				var option = ecisc.getOption();
				option.series[0].map = targPinyin;
				option.series[0].name = targName;
				option.series[0].data = mapMakerObj.parDataList[targPinyin];
				
				//街道名称隐藏
				if(mapMakerObj.level<3){
					option.series[0].label.normal.show = true;
				}
				
				var mapChart = echart3.init($('#mapJs')[0]);
				mapChart.setOption(option);
				
				bindMapEvents({
					mapChart:mapChart
				});
				callback();
				
				if(mapMakerObj.level<2){
					$('.map-toolbar .tw-tabMap ul li').eq(0).addClass('on').siblings('li').removeClass('on');
				}
				if(mapMakerObj.level==2){
					$('.map-toolbar .tw-tabMap ul li').eq(1).addClass('on').siblings('li').removeClass('on');
				}
				
				
				//part-new-select
				if(targName=='南通市'){
					showNanTongFourChild();
				}else{
					$.fn.clearMapNtChildTabs();
				}//end
			}
			
			
		}
		
		function backMapToJiangSu(callback){
			var targName = '江苏';
			var targPinyin = $Fun.pinyin.getFullChars(targName).toLowerCase();
			//part-new-select
			$.fn.clearMapNtChildTabs();//end
			
			mapMakerObj.level = 1;
			var ecisc = echart3.getInstanceByDom($('#mapJs')[0]);
			var option = ecisc.getOption();
			option.series[0].map = targPinyin;
			option.series[0].name = targName;
			option.series[0].data = mapMakerObj.parDataList[targPinyin];
			option.series[0].label.normal.show = true;
			
			var mapChart = echart3.init($('#mapJs')[0]);
			mapChart.setOption(option);
			
			bindMapEvents({
				mapChart:mapChart
			});
			callback();
		}
		
		function createMapBarLayer(data){
			if(data && data.areaData && data.areaData.length>0){
				var nd = [];
				$.each(data.areaData, function(i,e) {
					nd.push({name:e.name,percent:e.value});
				});
				var dataPm = { areaColor:"#ffab27", lineColor:"#ffab27", mData:nd };
				//柱状图生成
				$Fun.drawChartNewStyle( '.botBody.active', dataPm, function(rtn){
					var option = rtn;
					var pmChart = echart3.init($('.botBody.active')[0]);
					pmChart.setOption(option, true);
				} );
			}else{
				var ecic = echart3.getInstanceByDom($('.botBody.active')[0]);
				if(ecic){
					ecic.clear();
				}
			}
		}
		
		//江苏省各市混合图生成及单市柱状图
		function createMapBar(data){
			if(data && data.areaData && data.areaData.length>0){
				var nd = [];
				$.each(data.areaData, function(i,e) {
					nd.push({name:e.name,percent:e.value});
				});
				var dataPm = { areaColor:"#ffab27", lineColor:"#ffab27", mData:nd };
				//柱状图生成
				$Fun.drawChartNewStyle( '.tw-chart-area.active .tw-chart-div', dataPm, function(rtn){
					var option = rtn;
					var pmChart = echart3.init($('.tw-chart-area.active .tw-chart-div')[0]);
					pmChart.setOption(option, true);
				} );
			}else{
				var ecic = echart3.getInstanceByDom($('.tw-chart-area.active .tw-chart-div')[0]);
				if(ecic){
					ecic.clear();
				}
			}
		}
		
		//混合图
		function createMixedChart(data,dom){
			if(data && data.areaData && data.areaData.length>0){
				var nd = [];
				$.each(data.areaData, function(i,e) {
					nd.push(e);
				});
				var dataPm = { lineColor:["#b03a5b","#26c50a"], barColor:["#ffab27","#8082e0"], mData:nd };
				//图option生成
				$Fun.drawChartMixedStyle( dataPm, function(rtn){
					var option = rtn;
					var pmChart = echart3.init($(dom)[0]);
					pmChart.setOption(option, true);
				} );
			}else{
				var ecic = echart3.getInstanceByDom($(dom)[0]);
				if(ecic){
					ecic.clear();
				}
			}
		}
	};
	return app;
});